package com.example.ai.inventory.controller;

import com.example.ai.inventory.entity.Category;
import com.example.ai.inventory.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类控制器
 */
@RestController
@RequestMapping("/api/categories")
public class CategoryController {
    
    @Autowired
    private CategoryService categoryService;
    
    /**
     * 获取所有分类（树形结构）
     */
    @GetMapping
    public List<Category> getAllCategories() {
        return categoryService.getAllCategories();
    }
    
    /**
     * 根据ID获取分类
     */
    @GetMapping("/{id}")
    public Category getCategoryById(@PathVariable Long id) {
        return categoryService.getCategoryById(id);
    }
    
    /**
     * 添加分类
     */
    @PostMapping
    public Category addCategory(@RequestBody Category category) {
        return categoryService.addCategory(category);
    }
    
    /**
     * 更新分类
     */
    @PutMapping("/{id}")
    public Category updateCategory(@PathVariable Long id, @RequestBody Category category) {
        category.setId(id);
        return categoryService.updateCategory(category);
    }
    
    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    public void deleteCategory(@PathVariable Long id) {
        categoryService.deleteCategory(id);
    }
    
    /**
     * 根据父分类ID获取子分类
     */
    @GetMapping("/parent/{parentId}")
    public List<Category> getCategoriesByParentId(@PathVariable Long parentId) {
        return categoryService.getCategoriesByParentId(parentId);
    }
    
    /**
     * 搜索分类
     */
    @GetMapping("/search")
    public List<Category> searchCategories(@RequestParam String keyword) {
        return categoryService.searchCategories(keyword);
    }
}